package com.tiancheng.onecode.controller.api.merchant;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tiancheng.onecode.entity.PlatformMerchantRegInvite;
import com.tiancheng.onecode.model.request.admin.PlatformMerchantRegInviteAddReq;
import com.tiancheng.onecode.model.request.admin.PlatformMerchantRegInviteUpdateReq;
import com.tiancheng.onecode.model.response.Result;
import com.tiancheng.onecode.service.IPlatformMerchantRegInviteService;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: kellen
 * @Description: 子商户自主入驻配置
 * @Date: create in 2024/12/23 16:36
 */
@Controller
@RequestMapping("/api/b/merchantRegInvite")
public class MerchantRegInviteApiMerchantController {

    @Resource
    private IPlatformMerchantRegInviteService platformMerchantRegInviteService;

    /**
     * 查询平台已配置的自主入驻配置
     *
     * @param platformCode 平台编码
     * @return
     */
    @RequestMapping(value = "/queryByPlatformCode/{platformCode}", method = RequestMethod.GET)
    @ResponseBody
    public Result<PlatformMerchantRegInvite> queryByPlatformCode(@PathVariable("platformCode") String platformCode) {
        final List<PlatformMerchantRegInvite> list = platformMerchantRegInviteService.list(new LambdaQueryWrapper<PlatformMerchantRegInvite>().eq(PlatformMerchantRegInvite::getPlatformCode, platformCode).eq(PlatformMerchantRegInvite::getIsDisable, 0));
        return Result.success(CollectionUtils.isEmpty(list) ? null : list.get(0));
    }

    /**
     * 添加自主入驻配置
     *
     * @param PlatformMerchantRegInviteAddReq 平台子商户入驻入口配置信息
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public Result<PlatformMerchantRegInvite> add(@RequestBody @Valid PlatformMerchantRegInviteAddReq PlatformMerchantRegInviteAddReq) {
        final PlatformMerchantRegInvite platformMerchantRegInvite = platformMerchantRegInviteService.addNew(PlatformMerchantRegInviteAddReq);
        return Result.success(platformMerchantRegInvite);
    }

    /**
     * 修改自主入驻配置
     *
     * @param platformMerchantRegInviteUpdateReq 平台子商户入驻入口配置信息
     * @return
     */
    @RequestMapping(value = "/update", method = RequestMethod.PUT)
    @ResponseBody
    public Result add(@RequestBody @Valid PlatformMerchantRegInviteUpdateReq platformMerchantRegInviteUpdateReq) {
        platformMerchantRegInviteService.updateRecord(platformMerchantRegInviteUpdateReq);
        return Result.success();
    }


}
